package com.example.demo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author liuhe
 * @date 2020/08/21
 */
public class AnnotationTest {

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Report {
        String name() default "report";

        int age() default 18;
    }

    public class Sub {
        @Report(name = "liuhe", age = 28)
        public void testSub() {
            System.out.println("testSub");
        }
    }

    public static void main(String[] args) throws NoSuchMethodException {
        boolean annotationPresent = Sub.class.getMethod("testSub").isAnnotationPresent(Report.class);
        System.out.println(annotationPresent);  //true

        Report testSub = Sub.class.getMethod("testSub").getAnnotation(Report.class);
        System.out.println(testSub.age() + "#" + testSub.name()); //28#liuhe
    }
}
